package com.bskj.bms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bskj.bms.bean.req.ShopListReqVo;
import com.bskj.bms.bean.req.ShopReqVo;
import com.bskj.bms.service.IBmsShopService;
import com.bskj.common.bean.model.BmsUser;
import com.bskj.common.bean.model.BmsUserRole;
import com.bskj.common.bean.model.CodeType;
import com.bskj.common.bean.model.CodeValue;
import com.bskj.common.bean.model.ParkingShop;
import com.bskj.common.dao.BmsUserDao;
import com.bskj.common.dao.BmsUserRoleDao;
import com.bskj.common.dao.CodeTypeDao;
import com.bskj.common.dao.CodeValueDao;
import com.bskj.common.dao.ParkingShopDao;
import com.bskj.common.dao.core.BaseDao;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.OrderEnum;
import com.bskj.common.dao.core.Page;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.enums.BmsUserTypeEnum;
import com.bskj.common.service.impl.BaseServiceImpl;
import com.bskj.common.utils.EncryptUtil;
import com.bskj.common.utils.StringUtils;

@Service
public class BmsShopServiceImpl extends BaseServiceImpl<ParkingShop> implements IBmsShopService{
	
	@Autowired
	private ParkingShopDao parkingShopDao;
	
	@Autowired
	private BmsUserDao bmsUserDao;
	
	@Autowired
	private CodeTypeDao codeTypeDao;
	
	@Autowired
	private CodeValueDao codeValueDao;
	
	@Autowired
	private BmsUserRoleDao bmsUserRoleDao;
	
	@Override
	public BaseDao<ParkingShop> getBaseDao() {
		
		return parkingShopDao;
	}
	
	@Override
	public List<CodeValue> findShopType() throws Exception {
		FindRequestEntity findRequest=new FindRequestEntity();
		findRequest.addWhere("code", WhereEnum.EQ, "shopType");
		List<CodeType> list=codeTypeDao.find(findRequest);
		List<CodeValue> codeValues=new ArrayList<>();
		if(list.size()==1){
			FindRequestEntity findRequestv=new FindRequestEntity();
			findRequestv.addWhere("type_id", WhereEnum.EQ, list.get(0).getId());
			findRequestv.addOrder("orders", OrderEnum.ASC);
			codeValues=codeValueDao.find(findRequestv);
		}
		return codeValues;
	}

	@Override
	public Map<String, Object> addShop(ShopReqVo req){
		Map<String, Object> meg=new HashMap<>();
		ParkingShop parkingShop=new ParkingShop();
		parkingShop=getParkingShops(req);
		parkingShop.setStatus(1);
		parkingShop.setCreateTime(new Date());
		parkingShop.setUpdateTime(new Date());
		try {
			parkingShopDao.insert(parkingShop);
			addUser(parkingShop.getPhone());
		} catch (Exception e) {
			meg.put("success",false);
			meg.put("meg","注册失败，手机号码已被注册过！");
			e.printStackTrace();
			return meg;
		}
		meg.put("success",true);
		meg.put("meg","商家注册成功！");
		return meg;
	}
	
	/**
	 *添加商铺的同时建立一个商家用户 
	 * @return
	 */
	private void addUser(String  phone) throws Exception {
		FindRequestEntity findReq=new FindRequestEntity();
		findReq.addWhere("phone", WhereEnum.EQ, phone);
		List<ParkingShop> parkingShops=parkingShopDao.find(findReq);
		BmsUser bmsUser=new BmsUser();
		for (ParkingShop parkingShop : parkingShops) {
			bmsUser.setCustId(parkingShop.getParkingLotCustomerId());
			bmsUser.setShopId(parkingShop.getId());
			bmsUser.setPropId(0);
			bmsUser.setEntNo(parkingShop.getPhone());
			bmsUser.setPassword(EncryptUtil.getMD5(parkingShop.getPhone()));
			bmsUser.setType(BmsUserTypeEnum.parking_shop_user.getKey());
			bmsUser.setStatus(parkingShop.getStatus());
			bmsUser.setCreateTime(new Date());
			bmsUser.setUpdateTime(new Date());
		}
		bmsUserDao.insert(bmsUser);
		BmsUserRole bmsUserRole=new BmsUserRole();
		bmsUserRole.setRoleId(5);
		bmsUserRole.setUserId(bmsUser.getId());
		bmsUserRole.setOperator(bmsUser.getCustId());
		bmsUserRole.setCreateTime(new Date());
		bmsUserRole.setUpdateTime(new Date());
		bmsUserRoleDao.insert(bmsUserRole);
	}

	@Override
	public Page<ParkingShop> findAllShop(ShopListReqVo req) throws Exception {
		FindRequestEntity findReq=new FindRequestEntity();
		findReq.addWhere("parking_lot_customer_id", WhereEnum.EQ, req.getCustId());
		if (!StringUtils.isBank(req.getName())) {
			findReq.addWhere("name", WhereEnum.LIKE,"%"+req.getName()+"%");
		}
		findReq.addOrder("create_time", OrderEnum.DESC);
		findReq.setPage(req.getPageNo(), req.getPageSize());
		Page<ParkingShop> resultPage=page(findReq);
		return resultPage;
	}

	@Override
	public ParkingShop findShopData(Integer shopId) throws Exception {
		ParkingShop parkingShop=parkingShopDao.get(shopId);
		CodeValue codeValue=codeValueDao.get(parkingShop.getType());
		parkingShop.setTypeName(codeValue.getName());
		return parkingShop;
	}

	@Override
	public Map<String, Object> updateShop(ShopReqVo req) {
		Map<String, Object> meg=new HashMap<>();
		ParkingShop parkingShop=new ParkingShop();
		parkingShop=getParkingShop(req);
		parkingShop.setId(req.getId());
		parkingShop.setUpdateTime(new Date());
		try {
			parkingShopDao.update(parkingShop);
		} catch (Exception e) {
			meg.put("success",false);
			meg.put("meg","系统正忙，修改失败！");
			e.printStackTrace();
			return meg;
		}
		meg.put("success",true);
		meg.put("meg","修改成功！");
		return meg;
	}
	
	private ParkingShop getParkingShops(ShopReqVo req) {
		ParkingShop parkingShop=new ParkingShop();
		parkingShop.setParkingLotCustomerId(req.getCustId());
		parkingShop.setName(req.getShopName());
		parkingShop.setLocation(req.getLocation());
		parkingShop.setPhone(req.getPhone());
		parkingShop.setType(req.getCodeValue());
		parkingShop.setOwnerName(req.getOwnername());
		return parkingShop;
	}
	
	private ParkingShop getParkingShop(ShopReqVo req) {
		ParkingShop parkingShop=get(req.getId());
		parkingShop.setParkingLotCustomerId(req.getCustId());
		parkingShop.setName(req.getShopName());
		parkingShop.setLocation(req.getLocation());
		parkingShop.setPhone(req.getPhone());
		parkingShop.setType(req.getCodeValue());
		parkingShop.setOwnerName(req.getOwnername());
		return parkingShop;
	}

	@SuppressWarnings("null")
	@Override
	public void delRule(Integer bmsId) throws Exception {
		FindRequestEntity findRequestEntity=new FindRequestEntity();
		findRequestEntity.addWhere("user_id", WhereEnum.EQ, bmsId);
		List<BmsUserRole> bmsUserRoles=bmsUserRoleDao.find(findRequestEntity);
		if(bmsUserRoles != null || bmsUserRoles.size() >0 ){
			for (BmsUserRole bmsUserRole : bmsUserRoles) {
				bmsUserRoleDao.del(bmsUserRole.getId());
			}
		}
	}
}
